var express = require('express')
  , app = express();

app.use(express.compress());
app.use(express.favicon(__dirname + '/public/favicon.ico'));
/**
app.set('view options', {
    open: '{{',
    close: '}}'
});
*/
app.set('view engine', 'html');
app.enable('view cache');
//app.engine('html', require('ejs').renderFile);
app.engine('.html', require('ejs').__express);
//app.set('view engine', 'jade');

app.set('views', __dirname + '/views');
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use(express.session({secret: '1234567890QWERTY'}));


//Param
app.param(function(name, fn){
  if (fn instanceof RegExp) {
	return function(req, res, next, val){
	  var captures;
	  if (captures = fn.exec(String(val))) {
		req.params[name] = captures;
		next();
	  } else {
		next('route');
	  }
	}
  }
});
app.param('num', /^\d+$/);
app.param('str', /^\w+$/);
// General

app.get('/', function(req,res){
	res.render('index', { title: 'Route Separation Example' });
});
//code
app.get('/code', function(req, res){
	res.render('code/index', req.query);
});
app.get('/code/editor', function(req, res){
	res.render('code/editor', req.query);
});
app.get('/code/menu', function(req, res){
	res.render('code/menu', req.query);
});
app.post('/code/dialog/login', function(req, res){
	res.render('login', req.query);
});
app.get('/action/openid/before_login', function(req, res){
	console.log(req.query.op);
	//res.send(req.query.op);
	//res.end(req.query.op);
	//res.json({success:1});
	res.render('auth/qqlogin', req.query);
});
app.post('/code/dialog/f1_help', function(req, res){
	res.render('help', req.query);
});
app.get('/test', function(req, res){
	res.render('test', req.query);
});

//errors
app.use(function(err, req, res, next){
  // treat as 404
  if (~err.message.indexOf('not found')) return next();
  // log it
  console.error(err.stack);
  // error page
  res.status(500).render('errors/5xx',{ error: err });
});
// assume 404 since no middleware responded
app.use(function(req, res, next){
  res.status(404).render('errors/404', { url: req.originalUrl });
});

app.listen(3100);
console.log('Express app started on port 3100');

